package com.example.back.impl;

import com.example.back.mapper.RoomMapper;
import com.example.back.model.Room;
import com.example.back.model.User;
import com.example.back.service.RoomService;
import org.hibernate.mapping.TableOwner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Override
    public List<Room> findAllRoom() {
        return roomMapper.findAllRoom();
    }

    @Override
    public List<Room> findByInformation(String information) {
        return roomMapper.findByInformation(information);
    }

    @Override
    public List<Room> roomType(String type) {
        return roomMapper.roomType(type);
    }

    @Override
    public List<Room> roomState(int state) {
        return roomMapper.roomState(state);
    }

    @Override
    public List<Room> roomCount(int roomC) {
        return roomMapper.roomCount(roomC);
    }

    @Override
    public String findAccount(int id) {
        return roomMapper.findAccount(id);
    }

    @Override
    public Boolean insertRoom(String name, double cost, String type, int maxCount) {
        if (roomMapper.insertRoom(name, cost, type, maxCount)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean updateRoom(int id, double cost) {
        if (roomMapper.updateRoom(id, cost)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean deleteRoom(int id) {
        if (roomMapper.deleteRoom(id)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String createRoom(String account, String name, int state, String remark) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if(roomMapper.createRoom(name, state, remark) && roomMapper.intoRoom(account, name, time))
            return "success";
        return "fail";
    }

    @Override
    public String joinRoom(String account, String name) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if(roomMapper.joinRoom(name) && roomMapper.intoRoom(account, name, time))
            return "success";
        return "fail";
    }

    @Override
    public String leaveRoom(String account, String name) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if(roomMapper.findRoomCount(name) == 1)roomMapper.updateRoomRemark(name);
        if(roomMapper.outRoom(account, name, time)){
            if(roomMapper.leaveRoom(name))return "success";
        }
        return "fail";
    }

    @Override
    public Room findAtRoom(String account) {
        return roomMapper.findAtRoom(account);
    }

    @Override
    public List<User> findRoomUser(String name) {
        return roomMapper.findRoomUser(name);
    }

    @Override
    public String findRoomName(int id) {
        return roomMapper.findRoomName(id);
    }
}
